'''
Created on Apr 10, 2011

@author: u074363
'''
import unittest
import duracloud.store_client


def masterSetUp(self):
    self.originalHTTP = duracloud.store_client.httplib2.Http.request
    duracloud.store_client.httplib2.Http.request = mockSucessHttp
    self.dcStoreClient = duracloud.store_client.client("localhost", 8080,
                                                           "durastore")


def masterTearDown(self):
    duracloud.store_client.httplib2.Http.request = self.originalHTTP
    self.dcStoreClient = None


def mockSucessHttp(self, url):
        return {'status': '200'}, "Initialization Successful"


class TestStoreClientInit(unittest.TestCase):

    def setUp(self):
        unittest.TestCase.setUp(self)
        masterSetUp(self)

    def tearDown(self):
        masterTearDown(self)
        unittest.TestCase.tearDown(self)

    def testClientExsists(self):
        self.assertIsInstance(self.dcStoreClient,
                              duracloud.store_client.client)
        self.assertEqual("localhost", self.dcStoreClient.host,
                          "Hostname set incorrectly")
        self.assertEqual(8080, self.dcStoreClient.port, "Port set incorrectly")
        self.assertEqual("durastore", self.dcStoreClient.basePath,
                         "basePath set incorrectly")

    def testInitializeStores(self):
        response, content = self.dcStoreClient.initializeStores()
        self.assertEqual("200", response['status'],
                         "Wrong status code from server")
        self.assertEqual("Initialization Successful", content,
                         "Wrong return content")


class TestStoreClientInitCache(unittest.TestCase):

    def setUp(self):
        unittest.TestCase.setUp(self)
        masterSetUp(self)
        self.dcStoreClient = duracloud.store_client.client("localhost", 8080,
                                                        basePath="durastore",
                                                        cache=".cache")

    def tearDown(self):
        masterTearDown(self)
        unittest.TestCase.tearDown(self)

    def testClientUsesCache(self):
        self.assertEqual(".cache", self.dcStoreClient.cache,
                         "Cache set incorrectly")

    def testInitializeStores(self):
        response, content = self.dcStoreClient.initializeStores()
        self.assertEqual("200", response['status'],
                         "Wrong status code from server")
        self.assertEqual("Initialization Successful", content,
                         "Wrong return content")


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testClientExsists']
    unittest.main()
